package com.apolo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;

public class Ubicacion extends Activity {

	private WebView mapa;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ubicacion);
		
		this.mapa = (WebView) findViewById(R.id.wvMapa);
		this.mapa.getSettings().setJavaScriptEnabled(true);
		this.mapa.getSettings().setGeolocationEnabled(true);
		String MAP_URL = "http://manizalesmas.gruporevive.co/Vista/Mapa.html";
		final ProgressBar progress = (ProgressBar) findViewById(R.id.progress);
		mapa.setWebViewClient(new WebViewClient() {
			@Override
			public void onPageFinished(WebView view, String url) {
				ubicarPuntos();
				progress.setVisibility(View.INVISIBLE);
			}

		});
		
		if (this.hayInternet()) {
			this.mapa.loadUrl(MAP_URL);
		}
		
		else{
			String mapaNoDisponible =     "<html><head></head><body>" +
		            "<b>Necesitas una conexión a internet para ver la ubicación de Apolo.</b>" +
		            "</body></html>";
		            mapa.loadDataWithBaseURL("file:///android_asset/", mapaNoDisponible, "text/html", "utf-8", null);
		}
	}
	
	private boolean hayInternet() {
		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		if (cm.getActiveNetworkInfo() != null
				&& cm.getActiveNetworkInfo().isAvailable()
				&& cm.getActiveNetworkInfo().isConnected()) {
			return true;
		} else {
			return false;
		}
	}
	
	public void informacion(View view){
		Intent intent = new Intent(this, Informacion.class);
		startActivity(intent);
		this.finish();
	}
	
	public void contacto(View view){
		Intent intent = new Intent(this, Contacto.class);
		startActivity(intent);
		this.finish();
	}
	
	public void cursos(View view){
		Intent intent = new Intent(this, Cursos.class);
		startActivity(intent);
		this.finish();
	}
	
	public void facebook(View view){
		Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/apolosoft?fref=ts"));
		startActivity(intent);
	}
	
	public void twitter(View view){
		Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/apolosoft"));
		startActivity(intent);
	}
	
	public void volverMenu(View view){
		this.finish();
	}
	
	private void ubicarPuntos() {
		JSONObject paqueteDatos = new JSONObject();
		JSONArray jSonConsulta = new JSONArray();
		JSONObject elemento = new JSONObject();
		try {
			elemento.put("descripcion", "Apolo");
			elemento.put("latitud", "5.069838");
			elemento.put("longitud", "-75.516974");
			jSonConsulta.put(elemento);
			paqueteDatos.put("puntos", jSonConsulta);
			String consulta = "'" + paqueteDatos.toString() + "'";
			String centerURL = "javascript:ubicarPuntos(" + consulta + ")";
			this.mapa.loadUrl(centerURL);
		} catch (JSONException e1) {
		}
	}
}
